home *** CD-ROM | disk | FTP | other *** search
- /* TasksAndErrors.h: Task and error notification routines for ProjectDrag
- *
- * A set of applets for drag and drop source control by Tim Maroney.
- * See develop, issue 23 for details.
- *
- * Built on DropShell by Leonard Rosenthol, Stephan Somogyi, and Marshall Clow,
- * and using the MoreFiles utilities by Jim Luther.
- *
- * This software is free, but don't modify and redistribute it without
- * changing the status window to indicate your name and your changes!
- */
-
- #include <Dialogs.h>
- #include <Errors.h>
- #include <LowMem.h>
- #include <ToolUtils.h>
-
- #include "PDUtilities.h"
- #include "TasksAndErrors.h"
- #include "DSGlobals.h"
-
-
- #define kResTextAlertID 207
-
- #define kStatusWindow 210
- #define kStatusInfo 1
- #define kStatusText 3
-
-
- typedef struct TaskStackEntry
- {
- struct TaskStackEntry *fNext;
- Str255 fMessage;
- } TaskStackEntry;
-
-
- typedef struct PersistentAnswerRecord
- {
- struct PersistentAnswerRecord *fNext;
- short fStrListID;
- short fStrIndex;
- ConfirmResponse fAnswer;
- } PersistentAnswerRecord;
-
-
- static DialogPtr pStatusWindow;
- static OSErr pTaskError;
- static Str255 pTaskErrorString;
- static Boolean pHaveErrorNumber = false;
- static Boolean pHaveErrorString = false;
- static TaskStackEntry *pTaskStack = NULL;
- static PersistentAnswerRecord *pPersistentAnswer = NULL;
-
-
- static void ResTextErrorAlert(short strListID, short strIndex, OSErr callerErr,
- StringPtr param1, StringPtr param2,
- StringPtr param3, StringPtr param4);
-
-
- static void InstallStatusWindow(void)
- {
- if (pStatusWindow == NULL)
- {
- short type;
- Handle h;
- Rect r;
- Str255 s;
-
- pStatusWindow = GetNewDialog(kStatusWindow, NULL, (WindowPtr)-1);
- GetDItem(pStatusWindow, kStatusInfo, &type, &h, &r);
- ReplaceInIndString(s, kProjectDragStrings, kTimsByline, LMGetCurApName(),
- NULL, NULL, NULL);
- SetIText(h, s);
- ShowWindow(pStatusWindow);
- DrawDialog(pStatusWindow);
- }
- }
-
-
- void SetStatusMessage(StringPtr message)
- {
- short type;
- Handle h;
- Rect r;
- Str255 s;
- unsigned char nullString[1];
-
- if (pStatusWindow == NULL)
- InstallStatusWindow();
-
- GetDItem(pStatusWindow, kStatusText, &type, &h, &r);
- GetIText(h, s);
- if (message == NULL)
- {
- nullString[0] = 0;
- message = nullString;
- }
- if (!EqualString(message, s, true, true))
- SetIText(h, message);
- }
-
-
- void TaskStart(short strListID, short strIndex, StringPtr param1, StringPtr param2,
- StringPtr param3, StringPtr param4)
- {
- Str255 message;
- TaskStackEntry *theEntry;
-
- ReplaceInIndString(message, strListID, strIndex, param1, param2, param3, param4);
- SetStatusMessage(message);
-
- /* push the task string onto the task stack */
- theEntry = (TaskStackEntry*)NewPtr(sizeof(TaskStackEntry));
- /* XXX uh-oh if there's no memory */
- BlockMoveData(message, theEntry->fMessage, message[0] + 1);
- theEntry->fNext = pTaskStack;
- pTaskStack = theEntry;
- }
-
-
- void TaskDone(void)
- {
- /* pop the task from the task stack */
- TaskStackEntry *theEntry = pTaskStack;
- pTaskStack = pTaskStack->fNext;
- DisposePtr((Ptr)theEntry);
-
- if (pTaskStack != NULL)
- {
- SetStatusMessage(pTaskStack->fMessage);
- }
- else if (!gDone)
- {
- /* put nothing in the status window */
- SetStatusMessage(NULL);
- }
- }
-
-
- void PopAllTasks(void)
- {
- /* if there was an error, show the alert */
- if (pHaveErrorNumber)
- {
- if (pTaskError != userCanceledErr)
- ResTextErrorAlert(kProjectDragStrings, kAnErrorOccured, pTaskError,
- pTaskStack->fMessage, NULL, NULL, NULL);
- }
- else if (pHaveErrorString)
- {
- ResTextErrorAlert(kProjectDragStrings, kAnErrorOccured, pTaskError,
- pTaskStack->fMessage, pTaskErrorString, NULL, NULL);
- }
- pHaveErrorNumber = pHaveErrorString = false;
-
- /* delete the task stack */
- while (pTaskStack)
- {
- TaskStackEntry *theEntry = pTaskStack;
- pTaskStack = pTaskStack->fNext;
- DisposePtr((Ptr)theEntry);
- }
-
- /* delete the persistent answer list */
- while (pPersistentAnswer)
- {
- PersistentAnswerRecord *theEntry = pPersistentAnswer;
- pPersistentAnswer = pPersistentAnswer->fNext;
- DisposePtr((Ptr)theEntry);
- }
-
- /* put nothing in the status window */
- SetStatusMessage(NULL);
- }
-
-
- OSErr RaiseErrorNumber(OSErr err)
- {
- /* mark the error for PopAllTasks to show */
- pHaveErrorNumber = true;
- pTaskError = err;
- return err;
- }
-
-
- void RaiseErrorString(short strListID, short strIndex,
- StringPtr param1, StringPtr param2,
- StringPtr param3, StringPtr param4)
- {
- /* mark the error for PopAllTasks to show */
- pHaveErrorString = true;
- ReplaceInIndString(pTaskErrorString, strListID, strIndex,
- param1, param2, param3, param4);
- }
-
-
- void ResTextErrorAlert(short strListID, short strIndex, OSErr callerErr,
- StringPtr param1, StringPtr param2,
- StringPtr param3, StringPtr param4)
- {
- Str255 message;
- Str255 errorString;
-
- if (callerErr != noErr)
- NumToString (callerErr, errorString);
- else
- errorString[0] = 0;
- ReplaceInIndString(message, strListID, strIndex, param1, param2, param3, param4);
- ParamText(message, errorString, NULL, NULL);
- (void) Alert ( kResTextAlertID, NULL );
- }
-
-
- void PersistentAnswer(short strListID, short strIndex, ConfirmResponse answer)
- {
- /* search the list for this entry */
- PersistentAnswerRecord *theEntry;
- for (theEntry = pPersistentAnswer; theEntry != NULL; theEntry = theEntry->fNext)
- {
- if (theEntry->fStrListID == strListID && theEntry->fStrIndex == strIndex)
- {
- /* found it */
- theEntry->fAnswer = answer;
- return;
- }
- }
-
- /* didn't find it, add one */
- theEntry = (PersistentAnswerRecord*)NewPtrClear(sizeof(PersistentAnswerRecord));
- if (theEntry == NULL) return;
- theEntry->fStrListID = strListID;
- theEntry->fStrIndex = strIndex;
- theEntry->fAnswer = answer;
- theEntry->fNext = pPersistentAnswer;
- pPersistentAnswer = theEntry;
- }
-
-
- Boolean GetPersistentAnswer(short strListID, short strIndex, ConfirmResponse *answer)
- {
- /* search the list for this entry */
- PersistentAnswerRecord *theEntry;
- for (theEntry = pPersistentAnswer; theEntry != NULL; theEntry = theEntry->fNext)
- {
- if (theEntry->fStrListID == strListID && theEntry->fStrIndex == strIndex)
- {
- /* found it */
- *answer = theEntry->fAnswer;
- return true;
- }
- }
- return false;
- }
-